package com.hnas.trade.dao.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.util.StringUtil;
import com.hnas.trade.dao.OrderDetailDao;
import com.hnas.trade.dto.DTOOrderDetail;
import com.hnas.trade.orm.OrderDetail;

@Component("orderDetailDao")
public class OrderDetailDaoImpl extends BaseDaoImpl<OrderDetail> implements OrderDetailDao{

	public OrderDetailDaoImpl() {
		super(OrderDetail.class);
	}

	@Override
	public List<OrderDetail> findByOrderNo(String orderNo) {
		return this.findByQuery("from OrderDetail t where t.orderNo=?1", orderNo);
	}

	@Override
	public OrderDetail saveOrUpdate(DTOOrderDetail dto) throws Exception {
		OrderDetail vo = null;
		if(dto != null){
			String id = dto.getId();
			if(StringUtil.isNotEmpty(id)){
				vo = this.find(id);
			}
			if(vo == null){//新增
				vo = new OrderDetail();
				BeanUtils.copyProperties(dto, vo);
				this.persist(vo);
			}else{//修改
				BeanUtils.copyProperties(dto, vo, new String[]{"createTime","creator"});
				this.merge(vo);
			}
		}
		return vo;
	}

	@Override
	public OrderDetail findByOrderNoAndItemId(String orderNo, String itemId) {
		String hql = "from OrderDetail t where t.orderNo=?1 and t.relateId = ?2";
		List<OrderDetail> findByQuery = this.findByQuery(hql, orderNo,itemId);
		if(findByQuery == null || findByQuery.size() == 0) return null;
		return findByQuery.get(0);
	}

}
